// Created by carolsail 

import { ENUM_UI_TYPE } from "../Enum";
import DataManager from "../manager/DataManager";
import BaseLayer from "./Baselayer";
import { StaticInstance } from "../StaticInstance";
import { privacyChannel } from "../Script/privacyChannel";
import UIManager from "../manager/UIManager";
import { privacyChannelVivo } from "../Script/privacyChannelVivo";
import { privacyChannelXM } from "../Script/privacyChannelXM";
import { privacyChannelOppo1 } from "../Script/privacyChannelOppo1";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoadingLayer extends BaseLayer {
    finishLoad = false

    @property(cc.Sprite)
    loadfill: cc.Sprite = null
    onLoad(): void {
        cc.find("Canvas/UI/LoadingLayer/bg_notice").active = false
        console.log("privacyChannel.channel", privacyChannel.channel)
        switch (privacyChannel.channel) {

            case "oppo":
                this.rate = 0.25
            case "oppo1":
                this.rate = 0.4
                break
            case "xm":
                this.rate = 0.3
                break
            case "vivo":
            case "vivo1":
                this.rate = 0.75
                break
        }
    }
    onEnable() {


    }
    rate = 0.25
    onDisable() { }
    vivoLoadFinish = false
    UImagerLoadFinsh = false

    update(dt: number) {
        console.log("this.rate", this.rate)
        this.loadfill.fillRange += dt * this.rate
        if (this.loadfill && this.node.active) {
            //
            switch (privacyChannel.channel) {
                case "oppo":
                case "oppo1":
                    if (privacyChannel.getChannel().getCtrl("p12") && this.loadfill.fillRange < 1) {
                        privacyChannel.getChannel().showVideo(() => {
                            // DataManager.instance.power += 1
                            // DataManager.instance.save()
                        });
                    }
                    if (this.loadfill.fillRange >= 1 && !this.finishLoad) {
                        this.finishLoad = true;
                        (privacyChannel.getChannel() as privacyChannelOppo1).loadingVideo = false
                        cc.find("Canvas/UI").getComponent(UIManager).init()
                    }
                    break
                case "xm":
                    if (this.loadfill.fillRange >= 0.99 && !this.UImagerLoadFinsh) {
                        this.loadfill.fillRange = 0.99
                    }
                    if (privacyChannel.getChannel().getCtrl("p12") && this.loadfill.fillRange < 1 && !this.vivoLoadFinish) {

                        this.vivoLoadFinish = true;
                        console.log("loading播放视频");
                        (privacyChannel.getChannel() as privacyChannelXM).showManyVideo();
                    }
                    if (this.loadfill.fillRange >= 1 && !this.finishLoad) {
                        this.finishLoad = true;
                        //(privacyChannel.getChannel() as privacyChannelXM).loadingVideo = false

                        cc.find("Canvas/UI").getComponent(UIManager).init()
                    }
                    break
                case "vivo":
                case "vivo1":
                    if (this.loadfill.fillRange >= 0.99 && !this.UImagerLoadFinsh) {
                        this.loadfill.fillRange = 0.99
                    }
                    if (privacyChannel.getChannel().getCtrl("p12") && this.loadfill.fillRange < 1 && !this.vivoLoadFinish) {
                        this.vivoLoadFinish = true;
                        (privacyChannel.getChannel() as privacyChannelVivo).showVideo(() => {

                        }, () => {
                            this.scheduleOnce(() => {
                                this.vivoLoadFinish = false;
                            }, 0.35)
                        });
                    }

                    if (privacyChannel.getChannel().getCtrl("p13") && this.loadfill.fillRange < 1 && !this.vivoLoadFinish) {
                        this.vivoLoadFinish = true;
                        (privacyChannel.getChannel() as privacyChannelVivo).showVideo(() => {

                        }, () => {
                            this.scheduleOnce(() => {
                                this.vivoLoadFinish = false;
                            }, 0.15)
                        });
                    }


                    if (this.loadfill.fillRange >= 1 && !this.finishLoad) {
                        this.finishLoad = true
                        cc.find("Canvas/UI").getComponent(UIManager).init()
                    }
                    break
                default:
                    this.loadfill.fillRange = DataManager.instance.loadingRate
                    break

            }



            // if(DataManager.instance.loadingRate >= 1){
            //     // menu已加载完毕
            //     if(StaticInstance.uiManager.isActive(ENUM_UI_TYPE.MENU)){
            //         this.hide()
            //     }
            // }
        }
    }
}
